<?php
namespace app\admin\controller;

use \think\Controller as Controller;
use \think\Cache as Cache;
use \think\Validate as Validate;

/**
 * 书本章节类
 * 
 * 
 * @author Aninet Sat Jun 30 16:03:50 2018
 */
class Chapter extends Base{

    /**
     * 书本类型显示页
     * 
     * @return 
     * @date Sat Jun 30 16:04:53 2018
     */
    public function index(){
        $model = new \app\admin\model\BookType;
        $data = $model->field('bt_id,bt_name')->paginate();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 获取到某个分类下的书本
     * 
     * @return ajax/html
     * @date Sat Jun 30 16:05:17 2018
     */
    public function getbook(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\Book;
            $book = $model->where(['bt_id'=>$post['id']])->select();
            if($book){
                $info['msg'] = '所有书本';
                $info['data'] = $book;
                echo $this->right($info);
            }else{
                $info['msg'] = "没有数据";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }
    public function chapter(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\BookChapter;
            $chapter = $model->where(['bk_id'=>$post['id']])->select();
            if($chapter){
                $info['msg'] = '所有章节';
                $this->assign('chapter',$chapter);
                $info['data'] = $this->fetch();
                echo $this->right($info);
            }else{
                $info['msg'] = "没有数据";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
        
    }

    /**
     * 添加章节页面
     * 
     * @return html
     * @date Sat Jun 30 16:06:13 2018
     */
    public function add(){
        $model = new \app\admin\model\Book;
        $data = $model->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 添加一条章节
     * 
     * @return json
     * @date Sat Jun 30 16:07:03 2018
     */
    public function insert(){
        $post = $this->req->post();
        $rule = [
            'title' => 'require',
        ];
        $validate = new Validate($rule);
        // $result = $validate->check($post);
        if($validate->check($post)){
            $model = new \app\admin\model\Chapter;
            $data = [
                'bk_id' => $post['bkid'],
                'chat_title' => $post['title'],
                'chat_content' => $post['content'],
                'chat_exercise' => $post['exercise'],
                'chat_sort' => $post['sort']
            ];
            // $result = $model->save($data);
            if($model->save($data)){
                echo $this->alert(1,'添加成功');
            } else {
                echo $this->alert(0,'添加失败');
            }
        } else{
            echo $this->alert(0,'没有输入名称');
        }
        return;
    }

    /**
     * 修改某章内容页面。
     * 
     * @return json
     * @date Sat Jun 30 16:08:56 2018
     */
    public function edit(){
        $get = $this->req->get();
        $validate = new Validate(['id'=>'require|number']);
        if($validate->check($get)){
            $model = new \app\admin\model\Book;
            $book = $model->select();
            $chapter_model = new \app\admin\model\BookChapter;
            $chapter = $chapter_model->get($get['id']);
            $this->assign('book',$book);
            $this->assign('chapter',$chapter);
            return $this->fetch();
        }else{
            return $this->error('没有数据');
        }
    }

    /**
     * 更新一条章节
     * 
     * @return json
     * @date Sat Jun 30 16:07:03 2018
     */
    public function update(){
        $post = $this->req->post();
        $rule = [
            'title' => 'require',
        ];
        // var_dump($post);exit();
        $validate = new Validate($rule);
        // $result = $validate->check($post);
        if($validate->check($post)){
            $model = new \app\admin\model\Chapter;
            $data = [
                'bk_id' => $post['bkid'],
                'chat_title' => $post['title'],
                'chat_content' => $post['content'],
                'chat_exercise' => $post['exercise'],
                'chat_sort' => $post['sort']
            ];
            if($model->where(['chat_id'=>$post['id']])->update($data)){
                echo $this->alert(1,'修改成功');
            } else {
                echo $this->alert(0,'修改失败');
            }
        } else{
            echo $this->alert(0,'没有输入名称');
        }
        return;
    }

    public function preview(){
        
    }

    /**
     * @name read
     * @return html
     * @author Aninet Wed Jul 11 14:31:52 2018
     */
    public function read(){
        
    }


    public function remove(){
        if($this->req->has('id')){
            $id = $this->req->post('id');
            $model = new \app\admin\model\BookChapter;
            // $result = $model->where(['chat_id' => $id])->delete();
            $result = $model::destroy(['chat_id'=>$id]);
            // var_dump($model->getLastSql());exit();
            if($result){
                $info =[
                    'msg' => '删除成功',
                    'state' => 1
                ];
            } else {
                $info =[
                    'msg' => '删除失败',
                    'state' => 0
                ];
            }
            return $this->send($info);
        }
    }
}

